package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.entity.Concern;
import com.kang.entity.ParkUser;
import com.kang.entity.SysUser;
import com.kang.mapper.ConcernMapper;
import com.kang.mapper.UserMapper;
import com.kang.service.ConcernService;
import com.kang.util.ThreadLocalUtil;
import com.kang.vo.ParamInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConcernServiceImpl extends ServiceImpl<ConcernMapper, Concern> implements ConcernService {

    @Autowired
    private ConcernMapper concernMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer insertConcern(Concern concern) {
        concern.setUserId(ThreadLocalUtil.getCurrentUser().getId());
        concern.setCreateTime(new Date());
        concern.setUpdateTime(new Date());
        return concernMapper.insert(concern);
    }

    @Override
    public Integer deleteConcern(Integer concernId) {
        QueryWrapper<Concern> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentUser().getId()).eq("concern_id",concernId);
        return baseMapper.delete(wrapper);
    }

    @Override
    public Page<Concern> getConcernCount(ParamInfo paramInfo) {
        Page<Concern> page = new Page<>(paramInfo.getPageNum(), paramInfo.getPageSize());
        QueryWrapper<Concern> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentUser().getId());
        Page<Concern> concernPage = baseMapper.selectPage(page, wrapper);
        //获取关注谁的id
        List<Integer> collect = concernPage.getRecords().stream().map(Concern::getConcernId).collect(Collectors.toList());
        if (collect.size() != 0) {
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",collect);
            List<SysUser> sysUsers = userMapper.selectList(queryWrapper);
            List<Concern> records = concernPage.getRecords();
            for (Concern record : records) {
                for (SysUser sysUser : sysUsers) {
                    if (record.getConcernId().equals(sysUser.getId())) {
                        record.setUsernme(sysUser.getUsername());
                        record.setHeadUrl(sysUser.getHeadUrl());
                        record.setDes(sysUser.getDescription());
                    }
                }
            }
            concernPage.setRecords(records);
        }
        return concernPage;
    }

    @Override
    public Page<Concern> getFenSiCount(ParamInfo paramInfo) throws Exception {
        Integer userId = ThreadLocalUtil.getCurrentUser().getId();
        Page<Concern> page = new Page<>(paramInfo.getPageNum(), paramInfo.getPageSize());
        QueryWrapper<Concern> wrapper = new QueryWrapper<>();
        wrapper.eq("concern_id",userId);
        Page<Concern> concernPage = baseMapper.selectPage(page, wrapper);
        //获取关注谁的id
        List<Integer> collect = concernPage.getRecords().stream().map(Concern::getUserId).collect(Collectors.toList());
        if (collect.size() != 0) {
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",collect);
            List<SysUser> sysUsers = userMapper.selectList(queryWrapper);
            List<Concern> records = concernPage.getRecords();
            for (Concern record : records) {
                for (SysUser sysUser : sysUsers) {
                    if (record.getUserId().equals(sysUser.getId())) {
                        record.setUsernme(sysUser.getUsername());
                        record.setHeadUrl(sysUser.getHeadUrl());
                        record.setDes(sysUser.getDescription());
                    }
                }
                //判断是否已经关注
                QueryWrapper<Concern> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("user_id",userId);
                objectQueryWrapper.eq("concern_id",record.getUserId());
                Integer count = concernMapper.selectCount(objectQueryWrapper);
                if (count != 0) {
                    record.setIfConcern(1);
                } else {
                    record.setIfConcern(0);
                }
            }
            concernPage.setRecords(records);
        }
        return concernPage;
    }
}
